package logic

import (
	"context"
	"strconv"
	"zg6/zg6_month/goodssrv/model"

	"zg6/zg6_month/goodssrv/goodssrv"
	"zg6/zg6_month/goodssrv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateGoodsLogic {
	return &CreateGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateGoodsLogic) CreateGoods(in *goodssrv.CreateGoodsReq) (*goodssrv.Sky, error) {
	// todo: add your logic here and delete this line
	goods := model.Goods{
		GoodsName:  in.Goods.GoodsName,
		GoodsSn:    in.Goods.GoodsSn,
		ClickNum:   in.Goods.ClickNum,
		SoldNum:    in.Goods.SoldNum,
		ShopPrice:  in.Goods.ShopPrice,
		GoodsStock: int64(in.Goods.GoodsStock),
		GoodsDesc:  in.Goods.GoodsDesc,
	}
	err := model.DB.Create(&goods).Error
	if err != nil {
		return nil, err
	}
	_, err = model.EsClient.Index().Index(model.ES_INDEX).Id(strconv.Itoa(int(goods.ID))).BodyJson(goods).Do(context.Background())
	if err != nil {
		return nil, err
	}
	return &goodssrv.Sky{}, nil
}
